﻿using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraNavBar;
using MM.Common.Base;
using MM.Common.DBBase;
using MM.Common.Setup;
using MM.Common.Support.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmMenuSetup : FrmSetupWithSelect
    {
        public FrmMenuSetup()
        {
            InitializeComponent();
        }
        private void AddBarItem()
        {
            this.nbiClear = new NavBarItem();
            this.nbiClear.Caption = "删图";
            this.nbiClear.Hint = "删除图片";
            this.nbiClear.LargeImage = Resources.Cut_32x32;
            this.nbiClear.Name = "nbiClear";
            this.nbiClear.Enabled = false;
            this.nbiClear.LinkClicked += new NavBarLinkEventHandler(this.nbiClear_LinkClicked);
            this.nbiFunction = new NavBarItem();
            this.nbiFunction.Caption = "功能";
            this.nbiFunction.Hint = "选择菜单对应的功能";
            this.nbiFunction.LargeImage = Resources.CustomPropertyEditors_Large;
            this.nbiFunction.Name = "nbiFunction";
            this.nbiFunction.Enabled = false;
            this.nbiFunction.LinkClicked += new NavBarLinkEventHandler(this.nbiFunction_LinkClicked);
            base.navBarControl1.Items.AddRange(new NavBarItem[] { this.nbiClear, this.nbiFunction });
            base.nbgFunction.ItemLinks.Insert(3, this.nbiClear);
            base.nbgFunction.ItemLinks.Insert(4, this.nbiFunction);
        }

        protected void DispInfo()
        {
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("system_code", base.mainCode.ToString()) });
            this.RetrieveData(parm);
            base.dtSetup.Columns["OPEN_FLAG"].DefaultValue = "N";
            base.dtSetup.Columns["SYSTEM_CODE"].DefaultValue = base.mainCode;
            MemoryStream stream = new MemoryStream();
            Resources.empty.Save(stream, ImageFormat.Bmp);
            byte[] buffer = stream.ToArray();
            for (int i = 0; i < base.dtSetup.Rows.Count; i++)
            {
                string str = base.dtSetup.Rows[i]["ICO"].ToString();
                string path = SystemInfo.RunPath.ToUpper() + @"BMP\" + str;
                if ((str == "") || !File.Exists(path))
                {
                    base.dtSetup.Rows[i]["PIC"] = buffer;
                }
                else
                {
                    stream = new MemoryStream();
                    new Bitmap(path).Save(stream, ImageFormat.Bmp);
                    base.dtSetup.Rows[i]["PIC"] = stream.ToArray();
                }
            }
            base.dtSetup.Columns["PIC"].DefaultValue = buffer;
            base.dtSetup.AcceptChanges();
        }
        protected void FrmMenu_Load(object sender, EventArgs e)
        {
            this.AddBarItem();
            base.gvSetup = this.gvData;
            string selsql = "SELECT SYSTEM_CODE ,ORDER_NO, TITLE, FUNCTION_CODE, ICO ,PROMPT ,OPEN_FLAG ,( Select FUNCTION_NAME FROM MXHLY_SYS_FUNCTION WHERE MXHLY_SYS_FUNCTION.FUNCTION_CODE = MXHLY_SYS_MENU.FUNCTION_CODE ) FUNCTION_NAME FROM MXHLY_SYS_MENU WHERE SYSTEM_CODE = :system_code ORDER BY ORDER_NO ASC ";
            base.setSql(selsql, "MXHLY_SYS_MENU", new string[] { "SYSTEM_CODE", "ORDER_NO" }, new string[] { "FUNCTION_NAME", "PIC" }, "ORDER_NO", "");
            this.dtSystem = new DataTable();
            SystemInfo.DbData.Retrieve("SELECT SYSTEM_CODE ,SYSTEM_NAME FROM MXHLY_SYS_SYSTEM ORDER BY SYSTEM_CODE", ref this.dtSystem);
            this.gcTitle.Tag = "ON";
            this.gcPrompt.Tag = "ON";
            //base.ZoomWin();
            this.selectFresh();
        }

        private void gvSetup_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            if (e.Column.Name == "gcOrderNo")
            {
                string str = this.gvData.GetFocusedRowCellValue("ORDER_NO").ToString();
                this.gcFunction.OptionsColumn.AllowFocus = str.Length > 1;
                this.nbiFunction.Enabled = str.Length > 1;
                this.gcOpenFlag.OptionsColumn.ReadOnly = str.Length < 1;
            }
        }

        private void gvSetup_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {
                string str = this.gvData.GetFocusedRowCellValue("ORDER_NO").ToString();
                string str2 = this.gvData.GetFocusedRowCellValue("ICO").ToString();
                this.gcFunction.OptionsColumn.AllowFocus = str.Length > 1;
                this.nbiFunction.Enabled = str.Length > 1;
                this.gcIco.OptionsColumn.AllowFocus = str.Length > 1;
                this.nbiClear.Enabled = (str.Length > 1) && (str2 != "");
                this.gcOpenFlag.OptionsColumn.ReadOnly = str.Length < 1;
            }
        }

        private void gvSetup_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.Column.Name == "gcFunction")
            {
                this.SelectFunction();
            }
            if (e.Column.Name == "gcIco")
            {
                this.SelectIco();
            }
        }

        private void nbiClear_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            DataRow focusedDataRow = this.gvData.GetFocusedDataRow();
            if ((focusedDataRow != null) && (focusedDataRow["ICO"].ToString() != null))
            {
                focusedDataRow["ICO"] = "";
                focusedDataRow["PIC"] = base.dtSetup.Columns["PIC"].DefaultValue;
            }
        }

        private void nbiFunction_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.SelectFunction();
        }

        protected override bool RetrieveData(DataParamCol parm)
        {
            if (!base.RetrieveData(parm))
            {
                return false;
            }
            if (!base.dtSetup.Columns.Contains("PIC"))
            {
                DataColumn column = new DataColumn("PIC")
                {
                    DataType = Type.GetType("System.Byte[]")
                };
                base.dtSetup.Columns.Add(column);
            }
            return true;
        }

        protected override bool selectFresh()
        {
            base.selectFresh();
            if (this.dtSystem.Rows.Count == 1)
            {
                base.mainCode = this.dtSystem.Rows[0]["SYSTEM_CODE"];
                this.Text = "菜单设置 --- " + this.dtSystem.Rows[0]["SYSTEM_NAME"].ToString();
                this.DispInfo();
                return true;
            }
            FrmSelectFromGrid grid = new FrmSelectFromGrid(this.dtSystem, new string[] { "代码", "系统名" }, new int[] { 30, 60 }, "子系统选择");
            if (grid.ShowDialog(this) != DialogResult.Cancel)
            {
                int choiceRow = grid.ChoiceRow;
                base.mainCode = this.dtSystem.Rows[choiceRow]["SYSTEM_CODE"];
                this.Text = "菜单设置 --- " + this.dtSystem.Rows[choiceRow]["SYSTEM_NAME"].ToString();
                grid.Dispose();
                this.DispInfo();
                return true;
            }
            base.mainCode = new object();
            return false;
        }

        private void SelectFunction()
        {
            DataRow focusedDataRow = this.gvData.GetFocusedDataRow();
            if (focusedDataRow != null)
            {
                if (focusedDataRow["ORDER_NO"].ToString().Trim().Length < 1)
                {
                    MessageBox.Show("请先填写序号");
                }
                else
                {
                    FunctionPara fpara = new FunctionPara("", "")
                    {
                        pCode = focusedDataRow["FUNCTION_CODE"].ToString()
                    };
                    FrmSelectFunction function = new FrmSelectFunction();
                    function.SetParameter(fpara);
                    switch (function.ShowDialog(this))
                    {
                        case DialogResult.OK:
                            focusedDataRow["FUNCTION_CODE"] = fpara.pCode;
                            focusedDataRow["FUNCTION_NAME"] = fpara.pName;
                            if (focusedDataRow["TITLE"].ToString() == "")
                            {
                                focusedDataRow["TITLE"] = fpara.pName;
                            }
                            break;

                        case DialogResult.Cancel:
                            focusedDataRow["FUNCTION_CODE"] = "";
                            focusedDataRow["FUNCTION_NAME"] = "";
                            break;
                    }
                    base.nbiSave.Visible = true;
                }
            }
        }

        private void SelectIco()
        {
            this.openFileDialog1.InitialDirectory = SystemInfo.RunPath + "BMP";
            DataRow focusedDataRow = this.gvData.GetFocusedDataRow();
            if ((focusedDataRow != null) && (focusedDataRow["ORDER_NO"].ToString().Length > 1))
            {
                this.openFileDialog1.FileName = focusedDataRow["ICO"].ToString();
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string fileName = this.openFileDialog1.FileName;
                    int index = fileName.IndexOf(@"\");
                    for (int i = fileName.IndexOf(@"\", (int)(index + 1)); i > 0; i = fileName.IndexOf(@"\", (int)(index + 1)))
                    {
                        index = i;
                    }
                    string str2 = fileName.Substring(0, index + 1);
                    fileName = fileName.Substring(index + 1);
                    if (str2.ToUpper() != (SystemInfo.RunPath.ToUpper() + @"BMP\"))
                    {
                        File.Copy(this.openFileDialog1.FileName, SystemInfo.RunPath + @"BMP\" + fileName, true);
                    }
                    focusedDataRow["ICO"] = fileName;
                    MemoryStream stream = new MemoryStream();
                    new Bitmap(SystemInfo.RunPath.ToUpper() + @"BMP\" + fileName).Save(stream, ImageFormat.Bmp);
                    focusedDataRow["PIC"] = stream.ToArray();
                    this.nbiClear.Enabled = false;
                    base.nbiSave.Visible = true;
                }
            }
        }
    }
}
